const { addClaims, getClaims, editClaims, deleteClaims, getClaimApply, noClaimsApply, getFinishClaimApply, okClaimsApply } = require('../dao/claimsDao');

//文件移动模块 入口
const { moveFiles, deleteFiles } = require('../utils/handleFiles');

//新增管理员
module.exports.addClaims = async claim => {
    const data = await addClaims(claim);
    console.log(data);
    if (data) {
        //上传头像：用临时文件夹保存用户上传文件  实现方案1
        if (data.claimsImages) {
            // 移动文件
            moveFiles({
                fromPath: './public/temporary',
                toPath: './public/images',
                filename: data.claimsImages
            })
            // 删除
            deleteFiles('./public/temporary');
        }
        return {
            message: '新增理赔成功',
            status: 1,
            data
        }
    }
    return {
        message: '新增失败',
        status: 0,
    }
}

//get claims
module.exports.getClaims = async () => {
    const data = await getClaims();
    if (data) {
        return {
            message: '获取理赔列表成功',
            status: 1,
            data
        }
    }
    return {
        message: '获取失败',
        status: 0,
    }
}

//edit claims
module.exports.editClaims = async (_id, claim) => {
    const data = await editClaims(_id, claim);
    if (data) {
        return {
            message: '修改成功',
            status: 1,
            data
        }
    }
    return {
        message: '修改失败',
        status: 0,
    }
}

//delete
module.exports.deleteClaims = async _id => {
    const data = await deleteClaims(_id);
    if (data) {
        return {
            message: '删除理赔成功',
            status: 1,
            data
        }
    }
    return {
        message: '删除失败',
        status: 0,
    }
}
//管理人员查看所有理赔
module.exports.getClaimApply = async (content) => {
    let data = await getClaimApply(content)
    // console.log(data);
    if (data.data) {
        return {
            code: 1,
            msg: '获取成功',
            data
        }
    }
    return {
        code: 0,
        msg: '获取失败'
    }
}
//管理人员不同意理赔
module.exports.noClaimsApply = async (content) => {
    let data = await noClaimsApply(content)
    // console.log(data);
    if (data.nModified) {
        return {
            code: 1,
            msg: '获取成功',
        }
    }
    return {
        code: 0,
        msg: '获取失败'
    }
}
//管理人员同意理赔
module.exports.okClaimsApply = async (content) => {
    let data = await okClaimsApply(content)
    // console.log(data);
    if (data.nModified) {
        return {
            code: 1,
            msg: '获取成功',
        }
    }
    return {
        code: 0,
        msg: '获取失败'
    }
}
//查看已审核的理赔
module.exports.getFinishClaimApply = async (content) => {
    let data = await getFinishClaimApply(content)
    // console.log(data);
    if (data.data) {
        return {
            code: 1,
            msg: '获取成功',
            data
        }
    }
    return {
        code: 0,
        msg: '获取失败'
    }
}